<?php

namespace app\admin\model;

use think\Config;
use think\Model;

class Orders extends Model
{
    protected $connection = 'database.cheat';
    // 表名
    protected $table = 'orders';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'pament_time_text',
        'pay_style_text',
        'status_text',
    ];

    //关联user
    public function users()
    {
        return $this->belongsTo('Users','user_id','id',[],'LEFT')->setEagerlyType(0);
    }
    public function orderss()
    {
        return $this->belongsTo('Orders','shop_id','id',[],'LEFT')->setEagerlyType(0);
    }
    public function getPayStyleList()
    {
        return ['1' => __('Pay_style 1'),'2' => __('Pay_style 2')];
    }
    public function getStatusList()
    {
        return ['1' => __('Status 1'),'2' => __('Status 2'),'3' => __('Status 3'),'4' => __('Status 4'),'5' => __('Status 5'),'6' => __('Status 6')];
    }

    public function getPamentTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pament_time']) ? $data['pament_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayStyleTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['pay_style']) ? $data['pay_style'] : '');
        $list = $this->getPayStyleList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPamentTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
